from PyQt4 import QtGui, QtCore

import downloader

class DownloadWidget(QtGui.QDialog):

    def __init__(self, parent, downloads):
        QtGui.QWidget.__init__(self, parent)
        lang = parent.lang
        self.setWindowTitle('PyTv ' + lang['download'] + 's')
        self._down = downloads
        self._download = downloader.Downloader()
        self._folder = parent.db.read_configuration()[6]
        #Create Layout
        v_box = QtGui.QVBoxLayout(self)
        #Links 
        self._listTor = QtGui.QListWidget()
        #Text Area
        self.textArea = QtGui.QTextEdit()
        self.textArea.setReadOnly(True)
        self.lineEdit = QtGui.QLineEdit('')
        self.lineEdit.setReadOnly(True)
        #Add to main layout
        v_box.addWidget(QtGui.QLabel(lang['downEpi']))
        v_box.addWidget(self._listTor)
        v_box.addWidget(QtGui.QLabel(lang['copyLinks']))
        v_box.addWidget(self.textArea)
        v_box.addWidget(QtGui.QLabel(lang['ifPass']))
        v_box.addWidget(self.lineEdit)
        
        for d in self._down:
            self._listTor.addItem(d[0])

        self.connect(self._listTor, QtCore.SIGNAL("itemSelectionChanged()"), self._download_torrent)

    def _download_torrent(self):
        pos = self._listTor.currentRow()
        if(pos > -1):
            if type(self._down[pos][1]) == list:
                self.textArea.setPlainText(self._down[pos][1][0].replace('\n', '\n\n'))
                self.lineEdit.setText(self._down[pos][1][1])
            else:
                self.textArea.setPlainText(self._down[pos][1])

    def closeEvent(self, e):
        e.ignore()
        self.done(0)
